/*
 进入预约列表显示所有预约信息
 */

let appointment_data = new Vue({
    el: "#aptable_wrapper",
    data: {
        appointments: [],
        date: "",
        time: "",
        pageInfo:""

    },
    methods: {
        //获取所有的预约集合
        loadAppointments: function (pageNum) {
            if(!pageNum){
                pageNum=1;
            }
           let pageSize=$("#select1").val()
            axios({
                url: "/add_appointment/getAppointments",
                method: "get",
                params : {
                    pageNum:pageNum,
                    pageSize:pageSize
                }
            }).then(function (r) {
                if(r.status==200){
                    appointment_data.appointments= r.data.list;
                    console.log(r.data.list)
                    appointment_data.pageInfo=r.data;
                }
                //遍历集合拆分时间形成日期和时间
                for (let i = 0; i < appointment_data.appointments.length; i++) {
                    let datetime = appointment_data.appointments[i].appointTime.split("T");
                    appointment_data.appointments[i].date = datetime[0];
                    appointment_data.appointments[i].time = datetime[1];

                }
            })
        },
        //编辑修改预约
        editAppointment: function (id,index) {
            let edit_app = new Vue({
                el: "#mymodal",
                data: {
                    name: "123",
                    phone: "321",
                    age: "25",
                    appointtime: "2021-06-15T18:30",
                    departmentId: "",
                    doctorId: "",
                    description: "",
                    sex: "",
                    userId: "",
                    Id: ''
                },
                methods: {
                    //单独在切换的页加载这个预约所有信息
                    loadThisAppointment: function () {
                        let a = appointment_data.appointments;
                        let i=index;
                                this.userId=a[i].userId;
                                this.Id=a[i].id;
                                //给该模块元素赋初始值,本可以用vue绑定,发现在这里不起作用,所以用了jquery
                                $("#name").attr("value", a[i].name);
                                $("#age").attr("value", a[i].age);
                                var gender = a[i].gender;
                                $("input[name='sex']").removeAttr("checked");
                                //加载预约科室的多选项
                                axios({
                                    url: "/add_appointment/department",
                                    method: "get",
                                }).then(function (r) {
                                    let d = r.data;
                                    //移除之前加载的选项,重新加载当前选项
                                    $("#selectDepartment option").remove();
                                    $("#selectDepartment").append("<option value='' id=''>请选择---</option>")
                                    for (let i = 0; i < d.length; i++) {
                                        let option="<option id='"+d[i].id+"'>" + d[i].officeName + "</option>"
                                        //如果该部门是之前选的,就显示为首选项
                                        if(a[index].officeId==d[i].id){
                                            option="<option id='"+d[i].id+"' selected>" + d[i].officeName + "</option>"
                                        }
                                        $("#selectDepartment").append(option)
                                    }

                                })
                        //预设好性别等一系列预约中的内容,等待修改
                                $("input[value=" + gender + "] ").attr("checked", "checked")
                                $("#phone").attr("value", a[i].phone);
                                $("#description").text(a[i].description);
                                $("#date").attr("value",a[i].appointTime)
                        //根据当前科室加载医生
                        this.loadDoctors(a[index].officeId)

                    },
                    loadDoctors:function (officeId) {

                            //先获取列表中的预约
                            let a = appointment_data.appointments;
                            //如果officeId不存在,说明是从该方法是从点击事件触发的,就获取相应的officeId
                            if(!officeId){
                                officeId=$("#selectDepartment option:selected").attr("id");

                            }
                            //如果officeId等于零,说明没有选择预约科室,需要清空之前的医生预选项
                            if(officeId==0){
                                $("#selectDoctor option").remove();
                                $("#selectDoctor").append("<option value='' >请选择---</option>")
                            }else{
                                axios({
                                    url:"/add_appointment/loadDoctors",
                                    method:"GET",
                                    //根据officeId获取医生们
                                    params:{officeId:officeId}
                                }).then(function(r){
                                    //清空当前的所有医生选项,因为之前有可能加载过其他部门的医生
                                    $("#selectDoctor option").remove();
                                    console.log("办公室id" +officeId)
                                    let doctors=r.data;
                                    var id;
                                    var name;
                                    //先生成一个"请选择---"选项
                                    $("#selectDoctor").append("<option value='' >请选择---</option>")
                                    //生成每个医生的选项,放进了医生id和医生姓名
                                    for(let i=0;i<doctors.length;i++){
                                        id=doctors[i].id;
                                        name=doctors[i].name;
                                        let option="<option value="+id+">"+name+"</option>"
                                        //如果该医生是之前选的就将其作为首选项
                                        if(a[index].doctorId==id){
                                            option="<option value="+id+" selected>"+name+"</option>"
                                        }
                                        //在select元素中放进这些选项
                                        $("#selectDoctor").append(option)
                                    }
                                })
                            }



                    },
                    //上传预约信息
                    submitAppointment:function() {
                        var msg="确认修改并提交?"
                        if(confirm(msg)==true){
                            let form=new FormData();
                            //从各相关元素中获取用户填入的信息并填入form表单
                            form.append("id",this.Id)
                            this.name=$("#name").val();
                            form.append("name",this.name);
                            this.phone=$("#phone").val();
                            form.append("phone",this.phone);
                            this.age=$("#age").val();
                            form.append("age", this.age);
                            form.append("userId", this.userId);
                            this.departmentId=$("#selectDepartment option:selected").attr("id");
                            form.append("officeId",this.departmentId )
                            this.doctorId=$("#selectDoctor option:selected").val();
                            console.log("上传的医生的id:"+this.doctorId )
                            form.append("doctorId",this.doctorId )
                            this.appointtime=$("#date").val();
                            form.append("appointtime",this.appointtime)
                            this.description=$("textarea").val();
                            form.append("description",this.description)
                            let selectGender= document.getElementsByName("sex");
                            //判断性别选项
                            for (let i = 0; i < selectGender.length; i++) {
                                if (selectGender.item(i).checked) {
                                    this.sex= selectGender.item(i).getAttribute("value")
                                }
                            }
                            form.append("sex",this.sex )
                            // 异步上传预约信息
                            axios({
                                url:"/add_appointment/updateAppointment",
                                method:"POST",
                                data: form
                            }).then(function (r) {
                                if(r.data=="修改成功") {
                                    // $("#mymodal").attr("style","display: none");
                                    $("#mymodal").hide();
                                    alert(r.data)
                                    location.href = "/pages/appointment/appointment-list.html";
                                }else {
                                    alert(r.data);
                                }
                            })
                        }

                    },
                    //重新填写信息
                    clearAll:function () {
                        console.log("清空所有" )
                        //清空所有已填信息
                        for(let i=0;i<2;i++){
                            $(".info").val("");
                        }
                        //清除查询出的医生选项
                        $("option[name='id']").remove();
                        //重新获取当前时间
                        this.getDate();
                    },
                    //获取当前时间
                    getDate:function() {
                        var today = new Date();
                        var yyyy = today.getFullYear();//获取当前年,月,日,时,分
                        var MM = today.getMonth() + 1;
                        var dd = today.getDate();
                        var hh = today.getHours();
                        var mm = today.getMinutes();
                        //如果月,日,时,分为个位数,就在前面加个0
                        if (MM < 10) {
                            MM = "0" + MM;
                        }
                        if(dd<10){
                            dd="0"+dd;
                        }
                        if(hh<10){
                            hh="0"+hh;
                        }
                        if(mm<10){
                            mm="0"+mm;
                        }
                        //最终得到的时间格式
                        this.appointtime = yyyy + "-" + MM + "-" + dd + "T" + hh + ":" + mm;
                        //返回给对应的元素
                        let dateControl = document.getElementById("date");
                        dateControl.value = this.appointtime;
                    }

                },

                created: function () {
                    this.loadThisAppointment();
                }
            })
        },
        //删除选中的预约
        deleteAppointment:function(id,name) {
            var msg=("确定删除患者:'"+name+"'的预约信息?")
            if(confirm(msg)==true){
                console.log("被删除的预约的id:"+id)
                axios({
                    url:"/add_appointment/deleteAppointment",
                    method:"get",
                    params:{
                        Id:id
                    }
                }).then(function(r){
                    if(r.data==1){
                        alert("删除成功")
                    }else{
                        alert(r.data);
                    }
                    location.href="/pages/appointment/appointment-list.html";
                })
            }


        }
    },
    created: function () {
        this.loadAppointments();


    }


})




